package cn.yuanqiao.cqbc.inspection.service.impl;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;

import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.cqbc.fileBackup.taskClass.FilebackupInspection;
import cn.yuanqiao.cqbc.inspection.taskClass.TaskInspection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Service;
import cn.yuanqiao.cqbc.inspection.mapper.RegularInspectionsMapper;
import cn.yuanqiao.cqbc.inspection.domain.RegularInspections;
import cn.yuanqiao.cqbc.inspection.service.IRegularInspectionsService;

import javax.annotation.Resource;

/**
 * 巡检定时任务Service业务层处理
 * 
 * @author xy
 * @date 2024-06-20
 */
@Service
public class RegularInspectionsServiceImpl implements IRegularInspectionsService 
{
    @Autowired
    private RegularInspectionsMapper regularInspectionsMapper;
    @Autowired
    private ApplicationContext applicationContext;

    @Resource
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;
    private Map<String, ScheduledFuture<?>> futureMap = new ConcurrentHashMap<>();
    /**
     * 查询巡检定时任务
     * 
     * @param id 巡检定时任务主键
     * @return 巡检定时任务
     */
    @Override
    public RegularInspections selectRegularInspectionsById(Long id)
    {
        return regularInspectionsMapper.selectRegularInspectionsById(id);
    }

    /**
     * 查询巡检定时任务列表
     * 
     * @param regularInspections 巡检定时任务
     * @return 巡检定时任务
     */
    @Override
    public List<RegularInspections> selectRegularInspectionsList(RegularInspections regularInspections)
    {
        regularInspections.setTenantCode(SecurityUtils.getTenantCode());
        return regularInspectionsMapper.selectRegularInspectionsList(regularInspections);
    }

    /**
     * 新增巡检定时任务
     * 
     * @param regularInspections 巡检定时任务
     * @return 结果
     */
    @Override
    public int insertRegularInspections(RegularInspections regularInspections)
    {

        Long id= SnowflakeIdGenerator.generateId();
        regularInspections.setId(id);
        regularInspections.setTenantCode(SecurityUtils.getTenantCode());
        int i=regularInspectionsMapper.insertRegularInspections(regularInspections);
        if(regularInspections.getStatus().equals("1")){
            //说明开启定时任务
            TaskInspection taskInspection = applicationContext.getBean("taskInspection", TaskInspection.class);
            taskInspection.taskInspection(regularInspections);
            ScheduledFuture scheduledFuture = threadPoolTaskScheduler.schedule(taskInspection, new org.springframework.scheduling.support.CronTrigger(regularInspections.getTimeDtask()));
            futureMap.put(id.toString(), scheduledFuture);


        }

        return i;
    }

    /**
     * 修改巡检定时任务
     * 
     * @param regularInspections 巡检定时任务
     * @return 结果
     */
    @Override
    public int updateRegularInspections(RegularInspections regularInspections)
    {
        RegularInspections old=regularInspectionsMapper.selectRegularInspectionsById(regularInspections.getId());
        if(old.getStatus().equals("1")&&regularInspections.getStatus().equals("0")){
            //关闭定时任务

            ScheduledFuture<?> future = futureMap.get(regularInspections.getId().toString());
            if (future == null) {
                // log.warn("任务[{}]已不在调度中，无法停止！", regularInspections.getId());
                //return "任务[" + newEntity.getId() + "]已不在调度中，无法停止！";
            } else {
                future.cancel(true);
                futureMap.remove(regularInspections.getId().toString());
                // log.info("任务[{}]已被停止！", regularInspections.getId());
                //return "任务[" + jobId + "]已被停止！";
            }

        }
        if(old.getStatus().equals("0")&&regularInspections.getStatus().equals("1")){
            //开启任务
            TaskInspection taskInspection = applicationContext.getBean("taskInspection", TaskInspection.class);
            taskInspection.taskInspection(regularInspections);
            ScheduledFuture   scheduledFuture = threadPoolTaskScheduler.schedule(taskInspection, new org.springframework.scheduling.support.CronTrigger(regularInspections.getTimeDtask()));
            futureMap.put(regularInspections.getId().toString(), scheduledFuture);

        }
        if(old.getStatus().equals("1")&&regularInspections.getStatus().equals("1")){
        //修改任务

            ScheduledFuture<?> future = futureMap.get(regularInspections.getId().toString());
            if (future == null) {
               // log.warn("任务[{}]已不在调度中，无法停止！", regularInspections.getId());
                //return "任务[" + newEntity.getId() + "]已不在调度中，无法停止！";
            } else {
                future.cancel(true);
                futureMap.remove(regularInspections.getId().toString());
               // log.info("任务[{}]已被停止！", regularInspections.getId());
                //return "任务[" + jobId + "]已被停止！";
            }
            TaskInspection taskInspection = applicationContext.getBean("taskInspection", TaskInspection.class);
            taskInspection.taskInspection(regularInspections);
            ScheduledFuture   scheduledFuture = threadPoolTaskScheduler.schedule(taskInspection, new org.springframework.scheduling.support.CronTrigger(regularInspections.getTimeDtask()));
            futureMap.put(regularInspections.getId().toString(), scheduledFuture);



        }

        int i= regularInspectionsMapper.updateRegularInspections(regularInspections);
        return i;
    }

    /**
     * 批量删除巡检定时任务
     * 
     * @param ids 需要删除的巡检定时任务主键
     * @return 结果
     */
    @Override
    public int deleteRegularInspectionsByIds(Long[] ids)
    {
        return regularInspectionsMapper.deleteRegularInspectionsByIds(ids);
    }

    /**
     * 删除巡检定时任务信息
     * 
     * @param id 巡检定时任务主键
     * @return 结果
     */
    @Override
    public int deleteRegularInspectionsById(Long id)
    {
        return regularInspectionsMapper.deleteRegularInspectionsById(id);
    }
}
